package com.football.springboot.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.football.springboot.entity.League;
import com.football.springboot.entity.LeagueTeam;
import com.football.springboot.entity.Team;
import com.football.springboot.mapper.LeagueMapper;
import com.football.springboot.mapper.LeagueTeamMapper;
import com.football.springboot.service.ILeagueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.football.springboot.service.ITeamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李越
 * @since 2023-02-17
 */
@Service
public class LeagueServiceImpl extends ServiceImpl<LeagueMapper, League> implements ILeagueService {

    @Resource
    LeagueTeamMapper leagueTeamMapper;


    @Resource
    LeagueMapper leagueMapper;

    @Resource
    ITeamService teamService;

    //编辑参加联赛的球队
    @Override
    public void setLeagueTeam(Integer leagueId,List<Integer> teamIds) {
        //1.先删除当前联赛id所有绑定的球队
        leagueTeamMapper.deleteByLeagueId(leagueId);

        //2.再把前端传过来的球队id数组绑定到当前联赛id上
        List<Integer> teamIdsCopy = CollUtil.newArrayList(teamIds);
        for (Integer teamId : teamIds){
            LeagueTeam leagueTeam = new LeagueTeam();
            leagueTeam.setLeagueId(leagueId);
            leagueTeam.setTeamId(teamId);
            leagueTeamMapper.insert(leagueTeam);
        }
    }

    //查看联赛绑定的球队
    @Override
    public List<Integer> getLeagueTeam(Integer leagueId) {
        return leagueTeamMapper.selectByLeagueId(leagueId);
    }

    @Override
    public List<Team> findTeamByLeagueId(Integer leagueId) {
        return leagueMapper.findTeamByLeagueId(leagueId);
    }


}
